/**
 * @Author : JiGuangJie
 * @Date : 2023/4/13
 * @Time : 下午2:18
 * @Version : 1.0.0
 * @description :
 */
const createSourceXYZ = (code: string, url: string): any => {
    // @ts-ignore
    return new ol.source.XYZ({
        url,
        wrapX: false
    });
};

const createSourceWMTS = (type: string, url: string, opts: any): any => {
    console.log(opts);
    // @ts-ignore
    const projection = ol.proj.get(type);
    const tileSize = 256;
    const projectionExtent = projection.getExtent();
    // @ts-ignore
    const origin = ol.extent.getTopLeft(projectionExtent);
    // @ts-ignore
    const size = ol.extent.getWidth(projectionExtent) / tileSize;
    const resolutions = new Array(18);
    const matrixIds = [];
    for (let z = 1; z < 19; z++) {
        resolutions[z - 1] = size / Math.pow(2, z - 1);
        matrixIds[z - 1] = (z - 1).toString();
    }
    // @ts-ignore
    const tileGrid = new ol.tilegrid.WMTS({
        origin,
        resolutions: resolutions,
        extent: projectionExtent,
        matrixIds: matrixIds
    });
    // @ts-ignore
    return new ol.source.WMTS({
        url,
        layer: opts.layer,
        matrixSet: opts.matrixSet,
        format: opts.format,
        projection: projection,
        tileGrid: tileGrid,
        style: opts.style,
        wrapX: false
    });
};

const createSource = (type: string, url: string, code: string, opts?: any) => {
    switch (type) {
        case 'xyz':
            return createSourceXYZ(code, url);
        case 'wmts':
            return createSourceWMTS(code, url, opts);
        default:
            break;
    }
};

export {
    createSource
};